<?php
/*
	本插件：$this->model('api')->方法名();
	其他地方调用：model('jy_job2dao/api')->方法名();
*/
namespace jy_job2dao;
defined('BY_JYA') or exit('error');
class api extends \plugin{
	public function __construct(){
		$config=plugin('jy_job2dao')->plugin_setting('basic');
		if(!$config['host'] || !$config['username'] || !$config['password'] || !$config['port'] || !$config['database'] || !$config['tablepre']){
			return '';
		}
		$config['charset']='utf8';
		$this->connect($config);
		$this->_db = new \DB($config);
	}
	public function getfuniacids(){
		$funiacids = $this->_db->getall('uni_account',array('name <>'=>''));
		foreach($funiacids as &$fv){
			$fv['id']=$fv['uniacid'];
			$fv['title']=$fv['name'].'('.$fv['id'].')';
		}
		return $funiacids;
	}
	public function daoru($post=array()){
		$fweb = $this->_db->get('uni_account',array('uniacid'=>$post['funiacid']));
		if(!$post['type']){
			$this->uptype0($post['funiacid']);
		}elseif($post['type']==1){
			$this->uptype1($post['funiacid']);
		}elseif($post['type']==2){
			$this->uptype2($post['funiacid']);
		}elseif($post['type']==3){
			$this->uptype3($post['funiacid']);
		}elseif($post['type']==4){
			$this->uptype4($post['funiacid']);
		}elseif($post['type']==5){
			$this->uptype5($post['funiacid']);
		}
		$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'funiacid'=>$post['funiacid'],
				'funiacidtitle'=>$fweb['name'],
				'type'=>$post['type'],
				'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_job2dao_log',$insert);
		return '';
	}
	//搬移企业数据
	public function uptype0($funiacid=0,$page=1){
		$pindex = max(1, intval($page));
		$psize = 200;
		if($_RQ['num']){
			$psize=$_RQ['num'];
		}
		$total='';
		$orderby='id desc';
		$list=$this->_db->getslice('weixinmao_zp_company',array('uniacid'=>$funiacid),array($pindex,$psize),$total,$fields,'',$orderby);
		foreach($list as $item){
			$city=$this->_db->get('weixinmao_zp_city',array('uniacid'=>$funiacid,'id'=>intval($item['cityid'])));
			$item['city']=$city['name'];
			$area=$this->_db->get('weixinmao_zp_area',array('uniacid'=>$funiacid,'id'=>intval($item['areaid'])));
			$item['district']=$area['name'];
			$merchid=model('jy_job2dao/merch')->onemerch($item);
		}
		if(count($list)>180){
			$page++;
			$url='jy_job2dao/updata/uppage/type=0&page='.$page.'&funiacid='.$funiacid;
			exi('开始同步下一批200个企业！请勿关闭网页！','',$url);
		}
		exi('操作成功！','','/updata/');
		return '';
	}
	//搬移企业账号数据
	public function uptype1($funiacid=0,$page=1){
		$isaddmember=pdo_count('jy_job2dao_member',array('uniacid'=>$_SESSION['uniacid']));
		if($isaddmember<2){
			exi('请先搬移粉丝数据！！','error');
		}
		$isaddmerch=pdo_count('jy_51job_merch',array('oid >'=>0,'uniacid'=>$_SESSION['uniacid']));
		if($isaddmerch<2){
			exi('请先搬移企业数据！！','error');
		}
		$pindex = max(1, intval($page));
		$psize = 1000;
		if($_RQ['num']){
			$psize=$_RQ['num'];
		}
		$total='';
		$orderby='id desc';
		$list=$this->_db->getslice('weixinmao_zp_companyaccount',array('uniacid'=>$funiacid),array($pindex,$psize),$total,$fields,'',$orderby);
		foreach($list as $item){
			$orderid=model('jy_job2dao/merch')->merch_account($item);
		}
		if(count($list)>980){
			$page++;
			$url='jy_job2dao/updata/uppage/type=1&page='.$page.'&funiacid='.$funiacid;
			exi('开始同步下一批1000个企业账号数据！请勿关闭网页！','',$url);
		}
		exi('操作成功！','','/updata/');
		return '';
	}
	//搬移职位数据
	public function uptype2($funiacid=0,$page=1){
		$isaddmerch=pdo_count('jy_51job_merch',array('oid >'=>0,'uniacid'=>$_SESSION['uniacid']));
		if($isaddmerch<2){
			exi('请先搬移企业数据！！','error');
		}
		$pindex = max(1, intval($page));
		$psize = 1000;
		if($_RQ['num']){
			$psize=$_RQ['num'];
		}
		$total='';
		$orderby='id desc';
		$list=$this->_db->getslice('weixinmao_zp_job',array('uniacid'=>$funiacid),array($pindex,$psize),$total,$fields,'',$orderby);
		foreach($list as $item){
			if($item['jobcateid']){
				$jobcate=$this->_db->get('weixinmao_zp_jobcate',array('uniacid'=>$funiacid,'id'=>intval($item['jobcateid'])));
				$item['jobgrouptitle']=$jobcate['name'];	
			}
			$city=$this->_db->get('weixinmao_zp_city',array('uniacid'=>$funiacid,'id'=>intval($item['cityid'])));
			$item['city']=$city['name'];
			$area=$this->_db->get('weixinmao_zp_area',array('uniacid'=>$funiacid,'id'=>intval($item['areaid'])));
			$item['district']=$area['name'];
			$orderid=model('jy_job2dao/merch')->merch_job($item);
		}
		if(count($list)>980){
			$page++;
			$url='jy_job2dao/updata/uppage/type=2&page='.$page.'&funiacid='.$funiacid;
			exi('开始同步下一批1000个企业职位数据！请勿关闭网页！','',$url);
		}
		exi('操作成功！','','/updata/');
		return '';
	}
	//搬移粉丝数据
	public function uptype3($funiacid=0,$page=1){
		$pindex = max(1, intval($page));
		$psize = 1000;
		if($_RQ['num']){
			$psize=$_RQ['num'];
		}
		$total='';
		$orderby='uid desc';
		$list=$this->_db->getslice('mc_members2',array('uniacid'=>$funiacid),array($pindex,$psize),$total,$fields,'',$orderby);
		$wechat=pdo_get('core_account_wechat',array('uniacid'=>$_SESSION['uniacid'],'isdefault'=>1));
		if($wechat){
			$wechat=pdo_get('core_account_wechat',array('uniacid'=>$_SESSION['uniacid']));
		}
		//print_r($list);exit;
		foreach($list as $item){
			$fans=$this->_db->get('mc_mapping_fans',array('uid'=>$item['uid'],'uniacid'=>$funiacid));
			if($item['uid']){
				$item['credit1']=$item['credit1'];
				$item['credit2']=$item['credit2'];
			}
			$uid=model('jy_job2dao/member')->onefans($item,$fans,$wechat['id']);
		}
		if(count($list)>980){
			$page++;
			$url='jy_job2dao/updata/uppage/type=3&page='.$page.'&funiacid='.$funiacid;
			exi('开始同步下一批1000个会员！请勿关闭网页！','',$url);
		}
		exi('操作成功！','','/updata/');
		return '';
	}
	//搬移会员数据
	public function uptype4($funiacid=0,$page=1){
		$isaddmember=pdo_count('jy_job2dao_member',array('uniacid'=>$_SESSION['uniacid']));
		if($isaddmember<2){
			exi('请先搬移粉丝数据！！','error');
		}
		$pindex = max(1, intval($page));
		$psize = 1000;
		if($_RQ['num']){
			$psize=$_RQ['num'];
		}
		$total='';
		$orderby='id desc';
		$list=$this->_db->getslice('weixinmao_zp_userinfo',array('uniacid'=>$funiacid),array($pindex,$psize),$total,$fields,'',$orderby);
		foreach($list as $item){
			$uid=model('jy_job2dao/member')->onemember($item);
		}
		if(count($list)>980){
			$page++;
			$url='jy_job2dao/updata/uppage/type=4&page='.$page.'&funiacid='.$funiacid;
			exi('开始同步下一批1000个会员！请勿关闭网页！','',$url);
		}
		exi('操作成功！','','/updata/');
		return '';
	}
	//搬移简历数据
	public function uptype5($funiacid=0,$page=1){
		$isaddmember=pdo_count('jy_job2dao_member',array('uniacid'=>$_SESSION['uniacid']));
		if($isaddmember<2){
			exi('请先搬移粉丝数据！！','error');
		}
		$pindex = max(1, intval($page));
		$psize = 500;
		if($_RQ['num']){
			$psize=$_RQ['num'];
		}
		$total='';
		$orderby='id desc';
		$list=$this->_db->getslice('weixinmao_zp_jobnote',array('uniacid'=>$funiacid),array($pindex,$psize),$total,$fields,'',$orderby);
		foreach($list as $item){
			if($item['jobcateid']){
				$jobcate=$this->_db->get('weixinmao_zp_jobcate',array('uniacid'=>$funiacid,'id'=>intval($item['jobcateid'])));
				$item['jobgrouptitle']=$jobcate['name'];
			}
			$city=$this->_db->get('weixinmao_zp_city',array('uniacid'=>$funiacid,'id'=>intval($item['cityid'])));
			$item['city']=$city['name'];
			$area=$this->_db->get('weixinmao_zp_area',array('uniacid'=>$funiacid,'id'=>intval($item['areaid'])));
			$item['district']=$area['name'];
			$orderid=model('jy_job2dao/member')->jianli($item);
		}
		if(count($list)>499){
			$page++;
			$url='jy_job2dao/updata/uppage/type=5&page='.$page.'&funiacid='.$funiacid;
			exi('开始同步下一批200个简历数据！请勿关闭网页！','',$url);
		}
		exi('操作成功！','','/updata/');
		return '';
	}
	private function connect($cfg){
		$this->tablepre = $cfg['tablepre'];
		if($cfg['replace']){
			$this->replace=str_split($cfg['replace']);
		}
		if(empty($cfg)) {
			exi("The master database is not found!",'error');
		}
		$link = mysqli_connect($cfg['host'], $cfg['username'], $cfg['password']);
		if(empty($link)){
			$error = mysqli_error();
			if($error){
				if (strpos($error, 'Access denied for user') !== false) {
					$error = '源数据库访问用户名或是密码错误. <br />';
				} else {
					$error = iconv('gbk', 'utf8', $error);
				}
			}else{
				$error = '源数据库访问异常！！';
			}
			exi($error,'error');
		}
	}
}
?>